﻿using System;
using System.Web.UI;
using JQuery.WebKit.Base;
using JQuery.WebKit.WebCommon;
using JQuery.WebKit.WebCommon.Attributes;

namespace JQuery.WebKit
{
    public enum ButtonType
    {
        None = 0,
        Submit = 1,
        Refresh = 2
    }

    public class JButton : JControl
    {
        public ButtonType Behavior
        {
            get;
            set;
        }

        public string OnClientClick { get; set; }

        public string TargetUpdater { get; set; }

        public JButton()
            : base("input")
        {

        }

        public JButton(string tag)
            : base("input")
        {

        }

        [JavaScript("label", Category = "options")]
        public string Text
        {
            get;
            set;
        }

        [JavaScript("defaultClass", Category = "options")]
        public string DefaultClass
        {
            get;
            set;
        }

        [JavaScript("focusClass", Category = "options")]
        public string FocusClass
        {
            get;
            set;
        }

        [JavaScript("hoverClass", Category = "options")]
        public string HoverClass
        {
            get;
            set;
        }

        [JavaScript("activeClass", Category = "options")]
        public string ActiveClass
        {
            get;
            set;
        }

        internal override PostType PostType
        {
            get { return Base.PostType.Value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (string.IsNullOrEmpty(base.Name))
            {
                base.Name = this.ID;
            }
            if (Width.IsEmpty)
            {
                base.Style.Add(HtmlTextWriterStyle.Width, "150px");
            }
            else
            {
                base.Style.Add(HtmlTextWriterStyle.Width, Width.ToString());
            }
            if (string.IsNullOrEmpty(this.Attributes["click"]))
            {
                switch (Behavior)
                {
                    case ButtonType.None:
                        if (!string.IsNullOrEmpty(OnClientClick))
                        {
                            this.Attributes["onclick"] += string.Format("{0};", OnClientClick);
                        }
                        break;
                    case ButtonType.Submit:
                        if (!string.IsNullOrEmpty(OnClientClick))
                        {
                            if (OnClientClick.Contains("confirm("))
                            {
                                this.Attributes["onclick"] = string.Format("var result={0}; if (result==true || result==undefined) {1};", OnClientClick, base.GetUpdaterScript(TargetUpdater, ScriptType.Submit));
                            }
                            else
                            {
                                this.Attributes["onclick"] = string.Format("{0}; {1};", OnClientClick, base.GetUpdaterScript(TargetUpdater, ScriptType.Submit));
                            }
                        }
                        else
                        {
                            this.Attributes["onclick"] = base.GetUpdaterScript(TargetUpdater, ScriptType.Submit);
                        }
                        break;
                    case ButtonType.Refresh:
                        if (!string.IsNullOrEmpty(OnClientClick))
                        {
                            if (OnClientClick.Contains("confirm("))
                            {
                                this.Attributes["onclick"] = string.Format("var result={0}; if (result==true || result==undefined) {1};", OnClientClick, base.GetUpdaterScript(TargetUpdater, ScriptType.Refresh));
                            }
                            else
                            {
                                this.Attributes["onclick"] = string.Format("{0}; {1};", OnClientClick, base.GetUpdaterScript(TargetUpdater, ScriptType.Refresh));
                            }
                        }
                        else
                        {
                            this.Attributes["onclick"] = base.GetUpdaterScript(TargetUpdater, ScriptType.Refresh);
                        }
                        break;
                }
            }
            this.Attributes["type"] = "button";
            string script = string.Format("$('#{0}').button({1});", this.ClientID, this.GetJSProperties("options"));
            this.Page.Resources.Add(new WebScriptResource(typeof(JButton), this.ClientID, ResourceType.Javascript, script, LoadEvent.DocumentReady));

            base.OnPreRender(e);
        }
    }
}
